<?php 
$GLOBALS['Md'] = "Exchange";
$GLOBALS['Pd'] = "B";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
include("main.obj.mex");
include("nav.views.mex");
MYTCONN();
if (isset($_POST['SaveOperations']) && isset($_POST['bpn_tax'])) {

// Save Profit Tax
	
	$CTaxProf = "INSERT INTO exchange_profit (profit_time, tax_bpn, operations_total,expenses_ref) VALUES ('{$GLOBALS['T']}','{$_POST['bpn_tax']}','0','1')";
	$TaxProf = $GLOBALS['MYT']->db_query($CTaxProf);
	
	$CallTaxProf = "SELECT ref FROM exchange_profit ORDER BY profit_time DESC LIMIT 0,1";
	$CallTax = $GLOBALS['MYT']->db_array($CallTaxProf);

if (!isset($_POST['profit_groups']) && isset($_POST['profit_day'])) { 

$profit_day = $_POST['profit_day'];
$n = count($profit_day);
 
    for ($s = 0; $s < $n; $s++)
   {
   	$currentDay = mktime(0,0,0,m($profit_day[$s]),d($profit_day[$s]),a($profit_day[$s]));
	$endDay = mktime(24,00,00,m($profit_day[$s]),d($profit_day[$s]),a($profit_day[$s]));

	$CCall = "SELECT * FROM exchange_operations WHERE operation_time BETWEEN '{$currentDay}' AND '{$endDay}' AND in_currency_ref = '{$_POST['Currency']}'
	AND profit_ref = '0'";
	$BCall = $GLOBALS['MYT']->db_array($CCall);
	
	for ($i = 1; $i <= $BCall[0]; $i++) { 
	$CBa = "UPDATE exchange_operations SET profit_ref = '{$CallTax[1]['ref']}' WHERE ref = '{$BCall[$i]['ref']}'";
	$Ba = $GLOBALS['MYT']->db_query($CBa);
	}
	$Count = $Count+$i;
}
}
 if (!isset($_POST['profit_operations']) && isset($_POST['profit_groups'])) { 
 
 $profit_groups = $_POST['profit_groups'];
 $profit_day_gr = $_POST['profit_day_gr'];
 $m = count($profit_groups);


     for ($s = 0; $s < $m; $s++)
   {
   
	$currentDay_gr = mktime(0,0,0,m($profit_day_gr[$s]),d($profit_day_gr[$s]),a($profit_day_gr[$s]));
	$endDay_gr = mktime(24,00,00,m($profit_day_gr[$s]),d($profit_day_gr[$s]),a($profit_day_gr[$s]));
	 
   	
	 
	$CCall2 = "SELECT * FROM exchange_operations WHERE operation_time BETWEEN '{$currentDay_gr}' AND '{$endDay_gr}' AND main_office_ref = '{$profit_groups[$s]}' AND in_currency_ref = '{$_POST['Currency']}'
	AND profit_ref = '0'";
	$BCall2 = $GLOBALS['MYT']->db_array($CCall2);
	
	for ($i = 1; $i <= $BCall2[0]; $i++) { 
	
	$CBa2 = "UPDATE exchange_operations SET profit_ref = '{$CallTax[1]['ref']}' WHERE ref = '{$BCall2[$i]['ref']}'";
	$Ba2 = $GLOBALS['MYT']->db_query($CBa2);
	}
	$Count = $Count+$i;
}
}
 if (isset($_POST['profit_operation'])) { $profit_operation = $_POST['profit_operation'];
 print $o = count($profit_operation);
 
     for ($s = 0; $s < $o; $s++)
   {
   	 
	$CCall3 = "SELECT * FROM exchange_operations WHERE ref = '{$profit_operation[$s]}'
	AND profit_ref = '0'";
	$BCall3 = $GLOBALS['MYT']->db_array($CCall3);
	
	for ($i = 1; $i <= $BCall3[0]; $i++) { 
	print $BCall3[$i]['ref']."<br>";
	$CBa3 = "UPDATE exchange_operations SET profit_ref = '{$CallTax[1]['ref']}' WHERE ref = '{$BCall3[$i]['ref']}'";
	$Ba3 = $GLOBALS['MYT']->db_query($CBa3);
	}
	$Count = $Count+$i;
}

}

// Update Profit Tax
	
	$CTaxProf = "UPDATE exchange_profit SET operations_total = '{$Count}' WHERE ref = '{$CallTax[1]['ref']}'";
	$TaxProf = $GLOBALS['MYT']->db_query($CTaxProf);
}
?>

<table width="100%"  border="0" cellpadding="10">
	<tr>
    <td>Pesquisar Opera&ccedil;&otilde;es</td>
  </tr>
  <?php if (isset($_GET['update']) && $_GET['update'] == "expenses") {?>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <?php } elseif(isset($_GET['update']) && $_GET['update'] == "profit") {?>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <?php } 
 
if (!isset($_POST['Search']) && !isset($_GET['update'])) { ?>
<form action="oper.php" method="post" enctype="multipart/form-data">
<tr><td>
<table width="100%" border="0" cellpadding="3" cellspacing="1">

<tr bgcolor="#C7C7C7">
	<td>N�mero</td>
	<td>Data</td>
	<td>Taxa de Venda</td>
	<td>Total de Opera��es</td>
	<td>Despesas</td>
</tr>
<?php
  
		$QCallMain = "SELECT exchange_profit.expenses_ref,
								exchange_profit.ref,
								exchange_profit.TimeProfit,
								exchange_profit.Rate,
								exchange_profit.operations_total,
								(exchange_expenses.tax_value+exchange_expenses.other) AS expenses
		 FROM exchange_profit
		INNER JOIN exchange_expenses ON exchange_expenses.ref = exchange_profit.expenses_ref
		ORDER BY TimeProfit DESC";
	
		$CallMain = $GLOBALS['MYT']->db_array($QCallMain);
			for ($m = 1; $m <= $CallMain[0]; $m++) { 
		
		echo "<tr bgcolor=\"#F2F2F2\"><td>".$CallMain[$m]['ref']."</td><td>".$CallMain[$m]['TimeProfit']."</td><td>".$CallMain[$m]['Rate']."</td><td>{$CallMain[$m]['operations_total']}</td><td>".number_format($CallMain[$m]['expenses'],2)."</td></tr>";
			}
// END Default View /////////////////////////////
?>
    </table></td>
  </tr>
	<tr>
	<td><input name="UpdateOperations" type="submit" id="UpdateOperations" value="Actualizar"></td>
	</tr>
</form> 
<?php } ?>
</table>